package com.ruoyi.app.controller;

import com.alibaba.fastjson2.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.app.domain.electricfence.ElectricFenceListParam;
import com.ruoyi.app.domain.electricfence.ElectricFenceUpdateParam;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.LatLng;
import com.ruoyi.common.core.page.AppPage;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.comlbs.FenceTypeEnum;
import com.ruoyi.system.domain.ElectricFence;
import com.ruoyi.system.service.CommonService;
import com.ruoyi.system.service.IElectricFenceService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 描述 APP电子围栏Controller
 * 作者 ZhangPing
 * 创建时间 2023/10/8 17:11
 */
@Slf4j
@RestController
@RequestMapping("/app/electricFence")
public class AppElectricFenceController extends BaseController {

    @Autowired
    private IElectricFenceService electricFenceService;
    @Autowired
    private CommonService commonService;

    @ApiOperation("围栏列表")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody ElectricFenceListParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<ElectricFence> fenceList = electricFenceService.selectList(new ElectricFence()
                .setName(param.getName()).setTypes(Arrays.asList(FenceTypeEnum.POLYGON.getCode(), FenceTypeEnum.LINE.getCode())));
        return success(new AppPage<>(new PageInfo<>(ElectricFence.toAppModel(fenceList))));
    }

    @ApiOperation("围栏列表(不分页)")
    @PostMapping("/getAllFence")
    public AjaxResult listApp() {
        List<ElectricFence> fenceList = electricFenceService.selectList(new ElectricFence());
        return success(ElectricFence.toAppModel(fenceList));
    }

    @ApiOperation("围栏详情")
    @GetMapping("/queryById")
    public AjaxResult query(Long id) {
        List<ElectricFence> fenceList = electricFenceService.selectList(new ElectricFence().setId(id));
        return success(ElectricFence.toAppModel(fenceList).get(0));
    }

    @ApiOperation("新增围栏")
    @Log(title = "电子围栏", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody ElectricFenceUpdateParam param) {
        return update(param.setId(null));
    }

    @ApiOperation("编辑围栏")
    @Log(title = "电子围栏", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody ElectricFenceUpdateParam param) {
        List<LatLng> latLngList = JSON.parseObject(param.getMapxyStr()).getList("aoLatLng", LatLng.class);
        String address = commonService.latLngToAddress(new LatLng(param.getCenterLat(), param.getCenterLng()));
        boolean res = electricFenceService.saveOrUpdateElectricFence(new ElectricFence()
                .setId(param.getId())
                .setCompanyId(param.getCompanyId())
                .setName(param.getName())
                .setType(param.getType())
                .setXyGcj02(latLngList)
                .setAddress(address));
        return success(res);
    }

    @ApiOperation("围栏重命名")
    @Log(title = "电子围栏", businessType = BusinessType.UPDATE)
    @PostMapping("/rename")
    public AjaxResult rename(@RequestBody ElectricFence electricFence) {
        return success(electricFenceService.updateById(new ElectricFence().setId(electricFence.getId()).setName(electricFence.getName())));
    }

    @ApiOperation("删除围栏")
    @Log(title = "电子围栏", businessType = BusinessType.DELETE)
    @GetMapping("/remove")
    public AjaxResult remove(Long id) {
        return toAjax(electricFenceService.removeById(id));
    }

}
